package com.neusoft.work.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.work.dao.CareLevelProjectDao;
import com.neusoft.work.dao.CareProjectDao;
import com.neusoft.work.po.CareLevelProject;
import com.neusoft.work.po.CareProject;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.CareProjectService;

@Service
public class CareProjectServiceImpl implements CareProjectService {

    @Autowired
    private CareProjectDao careProjectMapper;
    @Autowired
    private CareLevelProjectDao clpMapper;

    @Override
    public PageResponseBean<List<CareProject>> queryByStatus(Integer status, Long cur, Long number,String name) {
        Page<CareProject> page = new Page<>(cur, number);
        QueryWrapper<CareProject> qw = new QueryWrapper<>();

        // 过滤逻辑删除
        qw.eq("isDeleted", false);
        if (status != null) {
            qw.eq("status", status);
        }
        // 默认只查询启用
        if (status == null) {
            qw.eq("status", 1);
        }
	    if (name != null && !name.trim().isEmpty()) {
	        qw.like("name", name.trim());
	    }
        Page<CareProject> result = careProjectMapper.selectPage(page, qw);
        PageResponseBean<List<CareProject>> resp = new PageResponseBean<>();
        resp.setData(result.getRecords());
        resp.setTotal(result.getTotal());
        return resp;
    }

    @Override
    public ResponseBean<Integer> addCareProject(CareProject careProject) {
        careProject.setIsDeleted(false);
        int rows = careProjectMapper.insert(careProject);
        ResponseBean<Integer> resp = new ResponseBean<>();
        resp.setData(careProject.getId());
        resp.setStatus(rows > 0 ? 200 : 500);
        resp.setMsg(rows > 0 ? "新增成功" : "新增失败");
        return resp;
    }

    @Override
    @Transactional
    public ResponseBean<Integer> updateCareProject(CareProject careProject) {
        // 如果状态从 启用→停用，需要清理所有级别关联
        if (careProject.getStatus() != null && careProject.getStatus() == 0) {
            clpMapper.delete(new QueryWrapper<CareLevelProject>()
                .eq("careProjectId", careProject.getId()));
        }
        int rows = careProjectMapper.updateById(careProject);
        if (rows > 0) {
            return new ResponseBean<>(careProject.getId());
        }
        return new ResponseBean<>(500,"更新失败");
    }

    @Override
    @Transactional
    public ResponseBean<Integer> deleteCareProject(Integer id) {
        // 逻辑删除
        CareProject cp = new CareProject();
        cp.setId(id);
        cp.setIsDeleted(true);
        int rows = careProjectMapper.updateById(cp);
        // 清理所有级别关联
        clpMapper.delete(new QueryWrapper<CareLevelProject>()
            .eq("careProjectId", id));
        if (rows > 0) {
            return new ResponseBean<>(id);
        }
        return new ResponseBean<>(500,"删除失败");
    }
}